﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharedClasses;

namespace UIDesigner.Designers.Controls
{
    public partial class ctrlLinkBuildLabel : UserControl
    {
        public event EventHandler LinkClicked;
        public event EventHandler Build;
        private bool m_bHasBuildButton;

        public ctrlLinkBuildLabel()
        {
            InitializeComponent();
        }

        private void ctrlLinkBuildLabel_Load(object sender, EventArgs e)
        {
            if (m_bHasBuildButton)
            {
                this.Controls.Add(this.cmdBuild);
                this.Width = cmdLink.Width + cmdBuild.Width + 5;
            }
            else
            {
                this.Width = cmdLink.Width;
            }
        }

        public bool LinkEnabled
        {
            get
            {
                return cmdLink.Enabled; 
            }

            set
            {
                cmdLink.Enabled = value;
            }
        }

        public bool HasBuildButton
        {
            get 
            { 
                return m_bHasBuildButton; 
            }
            
            set 
            { 
                m_bHasBuildButton = value;
            }
        }

        public override string Text
        {
            get
            {
                return cmdLink.Text; 
            }
        
            set
            {
                cmdLink.Text = value;
            }
        }

        private void cmdLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LinkClicked(this, e);
        }

        private void cmdBuild_Click(object sender, EventArgs e)
        {
            Build(this, e);
        }

        private void cmdBuild_Paint(object sender, PaintEventArgs e)
        {
            int hdc = e.Graphics.GetHdc().ToInt32();
            int crBack = VBUtils.ToColorRef(SystemColors.ButtonFace);
            int crBackOld;
            int hBrush = WindowsAPI.CreateSolidBrush(crBack);
            int hOldBrush;
            WindowsAPI.RECT rc = RectMath.ConvertToAPI(cmdBuild.ClientRectangle);  
            string strText = "...";

            hOldBrush = WindowsAPI.SelectObject(hdc, hBrush);
            crBackOld = WindowsAPI.SetBkColor(hdc, crBack);

            rc.Top--;

            WindowsAPI.Rectangle(hdc, 0, 0, cmdBuild.Width, cmdBuild.Height);
            WindowsAPI.DrawText(hdc, ref strText, strText.Length, ref rc, WindowsAPI.DrawTextFormats.DT_CENTER | WindowsAPI.DrawTextFormats.DT_SINGLELINE | WindowsAPI.DrawTextFormats.DT_VCENTER);
            WindowsAPI.DrawEdge(hdc, ref rc, WindowsAPI.BorderStyles.EDGE_RAISED, WindowsAPI.BorderFlags.BF_RECT);

            WindowsAPI.SetBkColor(hdc, crBackOld);
            WindowsAPI.SelectObject(hdc, hOldBrush);
            WindowsAPI.DeleteObject(hBrush); 

            e.Graphics.ReleaseHdc();
        }
    }
}
